﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Agnes.Core;
using ICSharpCode.Core;
using Ionic.Zip;
using Microsoft.Win32;

namespace Agnes.Mainframe
{
    /// <summary>
    /// 打开解决方案 ViewModel
    /// </summary>
    public class OpenSlnViewModel
    {
        /// <summary>
        /// 最近解决方案列表
        /// </summary>
        public List<String> RecentlyUsedSlns { get; set; }

        /// <summary>
        /// 被选中的最近解决方案
        /// </summary>
        public string SelectedUsedSln { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public OpenSlnViewModel()
        {
            RecentlyUsedSlns = AgnesServiceManager.AgnesSlnService.RecentlyUsedSlns;
        }

        /// <summary>
        /// 响应双击最近解决方案列表中的项
        /// </summary>
        public void OpenRecentlySln()
        {
            if (!string.IsNullOrEmpty(SelectedUsedSln))
            {
                if (MessageBox.Show("你确定要打开解决方案:" + SelectedUsedSln + "?", "提示", MessageBoxButton.YesNo)
                     == MessageBoxResult.No)
                {
                    return;
                }

                //检查当前解决方案路径是否有效
                if (!File.Exists(SelectedUsedSln))
                {
                    MessageBox.Show("当前解决方案不存在可能已经被删除或者移动！", "Agnes Warning");
                    return;
                }

                if (AgnesServiceManager.AgnesSlnService.CurrentSln != null)
                {
                    //判断是否是当前解决方案
                    if (AgnesServiceManager.AgnesSlnService.CurrentSln.FullName == SelectedUsedSln)
                    {
                        return;
                    }

                    //提示用户是否保存方案
                    MessageBoxResult res = MessageBox.Show("是否保存 "
                                                              + AgnesServiceManager.AgnesSlnService.CurrentSln.SlnName + "?", "温馨提示",
                                                              MessageBoxButton.YesNoCancel);

                    if (res == MessageBoxResult.Cancel)
                    {
                        return;
                    }

                    if (res == MessageBoxResult.Yes)
                    {
                        AgnesServiceManager.AgnesSlnService.CurrentSln.Save();
                    }

                    AgnesServiceManager.AgnesSlnService.OnSlnClosed(null, null);
                }
                //打开解决方案
                try
                {
                    AgnesServiceManager.AgnesSlnService.OpenSln(SelectedUsedSln);
                }
                catch (BadPasswordException)
                {
                    MessageBox.Show("该方案被密码保护了,请输入正确密码!", "密码错误");
                    ErrPwd(SelectedUsedSln);
                }

            }
        }

        /// <summary>
        /// 响应打开解决方案按钮
        /// </summary>
        public void OpenSln()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = PropertyService.Get("DefaultSlnDir");
            ofd.Filter = "(Agnes解决方案)|*.agsln";
            ofd.Multiselect = false;
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == true)
            {
                PropertyService.Set("DefaultSlnDir", ofd.FileName.Substring(0,
                    ofd.FileName.LastIndexOf("\\")));
                try
                {
                    AgnesServiceManager.AgnesSlnService.OpenSln(ofd.FileName);
                }
                catch (BadPasswordException)
                {
                    MessageBox.Show("该方案被密码保护了,请输入正确密码!", "密码错误");
                    ErrPwd(ofd.FileName);
                }
            }
        }

        private void ErrPwd(string filename)
        {
            PwdInputView view = new PwdInputView();
            view.ShowDialog();
            if (view.DialogResult == true)
            {
                try
                {
                    AgnesServiceManager.AgnesSlnService.OpenSln(filename, view.Password);
                }
                catch (BadPasswordException)
                {
                    MessageBox.Show("密码错误,请重新输入!");
                    ErrPwd(filename);
                }
            }
        }
    }
}
